package cn.itcast.quartz.scheduler;

import cn.itcast.quartz.job.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class HelloSchedulerDemo {

    public static void main(String[] args) throws Exception {
        //1：从工厂中获取任务调度的实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        // 2：定义一个任务调度实例，将该实例与HelloJob绑定，任务类需要实现Job接口
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("helloJob", "helloJobGroup")
                .usingJobData("count",0)
                .build();

//        System.out.println(jobDetail.getKey().getName());
//        System.out.println(jobDetail.getKey().getGroup());
//        System.out.println(jobDetail.getKey().getClass());

        // 3：定义触发器 ，马上执行, 然后每5秒重复执行一次
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("helloTrigger", "helloTriggerGroup")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(5))
                .usingJobData("message","this is trigger")
                .build();

        // 4：使用触发器调度任务的执行
        scheduler.scheduleJob(jobDetail, trigger);

        // 5：开启
        scheduler.start();
//        scheduler.shutdown();;

    }
}
